package com.qzsoft.system.modules.permission.group.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qzsoft.jeemis.common.annotation.LogOperation;
import com.qzsoft.jeemis.common.constant.Constant;
import com.qzsoft.jeemis.common.utils.ConvertUtils;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.jeemis.common.validator.ValidatorUtils;
import com.qzsoft.jeemis.common.validator.group.DefaultGroup;
import com.qzsoft.jeemis.common.validator.group.UpdateGroup;
import com.qzsoft.system.modules.permission.group.dto.SysGroupDTO;
import com.qzsoft.system.modules.permission.group.entity.SysGroupEntity;
import com.qzsoft.system.modules.permission.group.service.SysGroupService;
import com.qzsoft.system.modules.permission.role.dto.SysRoleDTO;
import com.qzsoft.system.modules.permission.role.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 用户组信息(Sys_group)表控制层
 */
@RestController
@RequestMapping("/sys/sys_group")
@Api(tags = "用户组管理")
@Log4j2
public class SysGroupController {
    @Autowired
    private SysGroupService service;

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 获取业务系统-菜单管理树
     *
     * @return
     */
    @GetMapping("group_tree")
    @ApiOperation("用户组表")
    public Result getSysGroupTree() {
        List<SysGroupDTO> data = service.getSysGroupTree();
        return new Result<List<SysGroupDTO>>().ok(data);
    }

    /**
     * （用户）分组角色分页
     *
     * @param page
     * @param dto
     * @return
     */
    @GetMapping("group_role_page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "link_name", value = "名字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "ge_createDate", value = "起始创建时间", paramType = "query", dataType = "Date"),
            @ApiImplicitParam(name = "le_createDate", value = "截止创建时间", paramType = "query", dataType = "Date")})
    @RequiresPermissions("sys:role:page")
    public Result<IPage<SysRoleDTO>> groupRolePage(Page page, SysRoleDTO dto) {
        IPage<SysRoleDTO> pageRst = sysRoleService.groupRolePage(page, dto);

        return new Result<IPage<SysRoleDTO>>().ok(pageRst);
    }

    /**
     * 保存用户分组角色
     *
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("保存用户分组角色")
    public Result save(@Valid @RequestBody SysGroupDTO dto) {
        SysGroupEntity data = ConvertUtils.sourceToTarget(dto, SysGroupEntity.class);
        return new Result().ok(service.save(data));
    }

    /**
     * 根据ID查询用户分组信息
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<SysGroupEntity> get(@PathVariable("id") String id) {
        SysGroupEntity data = service.get(id);
        return new Result<SysGroupEntity>().ok(data);
    }

    /**
     * 修改用户分组信息
     *
     * @param dto
     * @return
     */
    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("sys:role:update")
    public Result update(@RequestBody SysGroupDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        return new Result().ok(service.update(dto));
    }

    /**
     * 删除用户分组
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@RequestBody String id) {
        service.delete(id);
        return new Result();
    }
}